<template>
  <div>
    <!-- 面包屑导航 -->
    <breadcrumbs :data1="data1" :data2="data2"></breadcrumbs>
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="getGoodsInfo.query" clearable @clear="queryGoods">
            <el-button slot="append" icon="el-icon-search" @click="queryGoods"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4" class="addgoodbtn">
          <el-button type="primary" @click="addGoodsPage">添加商品</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-table :data="tableData" border stripe style="width: 100%">
          <el-table-column type="index" label="序号" width="60"></el-table-column>
          <el-table-column prop="goods_name" label="商品名称" width="700"></el-table-column>
          <el-table-column prop="goods_price" label="商品价格(单位:元)"></el-table-column>
          <el-table-column prop="goods_weight" label="商品重量(单位:g)"></el-table-column>
          <el-table-column prop="add_time" label="创建时间">
            <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-tooltip effect="dark" content="编辑" placement="top">
                <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="删除" placement="top">
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  @click="delGoods(scope.row.goods_id)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="getGoodsInfo.pagenum"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="getGoodsInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-row>
    </el-card>
  </div>
</template>
           
<script>
import breadcrumbs from '@/common/breadcrumbs.vue'

export default {
  data () {
    return {
      data1: '商品管理',
      data2: '商品列表',
      getGoodsInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      tableData: [],
      total: 0
    }
  },
  components: {
    breadcrumbs
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.getGoodsInfo })
      this.tableData = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange (val) {
      this.getGoodsInfo.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange (val) {
      this.getGoodsInfo.pagenum = val
      this.getGoodsList()
    },
    async queryGoods () {
      this.getGoodsList()
    },
    async delGoods (id) {
      const { data: res } = await this.$http.get(`goods/${id}`)
      this.$confirm(`此操作将永久删除商品: <strong style="color:red">'${res.data.goods_name}'</strong> , 是否继续?`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        dangerouslyUseHTMLString: true
      }).then(async () => {
        const { data: res } = await this.$http.delete(`goods/${id}`)
        if (res.meta.status === 200) {
          this.$message.success('商品删除成功~~')
          this.getGoodsList()
        } else {
          this.$message.error(res.meta.msg)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '用户取消操作'
        });
      });
    },
    addGoodsPage(){
      this.$router.push('/goods/add')
    }
  }

}
</script>
<style scoped lang='less'>
.addgoodbtn {
  margin-left: 15px;
}
</style>